<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of settings_m
 *
 * @author QUYET
 */
class news_m extends CI_Model {
    var $language = 1;
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
        if($this->session->userdata('lang')=='en') $this->language = 2;
    }
    function get_aboutus(){
        $this->db->select('*');
        $this->db->join('news_lang','news.id=news_lang.id_news','join');
        $this->db->where('news_lang.id_lang',$this->language);
        $this->db->where('id',1);        
        $abouts_all = $this->db->get('news')->result();
        if(!empty($abouts_all[0])) return $abouts_all[0];
        return false;
    }
    
    function get_allnews($per_page=10,$off_set=0){
        $this->db->select('*');
        $this->db->join('news_lang','news.id=news_lang.id_news','join');
        $this->db->where('id !=',1);
        $this->db->where('news_lang.id_lang',$this->language);
        $this->db->order_by('id','desc');
        $this->db->limit($per_page,$off_set);
        return $this->db->get('news')->result();
        
        
    }
    
    function count_all_news(){
        $this->db->select('id');
        $this->db->join('news_lang','news.id=news_lang.id_news','join');
        $this->db->where('id !=',1);
        $this->db->where('news_lang.id_lang',$this->language);
        return count($this->db->get('news')->result_array());
    }
    
    function get_onenews($id, $uselang = false){
        if(!is_numeric($id)) return false;
        $this->db->select('*');
        $this->db->from('news');
        $this->db->join('news_lang','news.id=news_lang.id_news','join');
        //$this->db->where('news_lang.id_lang',$this->language);
        $this->db->where('id',$id);
        
        if($uselang) {
            $this->db->where('news_lang.id_lang',$this->language);
            return $this->db->get()->row();
        }
        else{
            $one_cate_alldetail = $this->db->get()->result();
            if(count($one_cate_alldetail)==2){
                foreach($one_cate_alldetail as $catlg){
                    if($catlg->id_lang==$this->language) return $catlg;
                }
            }
            elseif(!empty($one_cate_alldetail[0])) return $one_cate_alldetail[0];
            else return false;
        }
    }
    
    function add_news($data){
        if(empty($data)) return false;        
        $record_news = array('news_date'=>date('Y-m-d H:i:s'));
        $this->db->insert('news',$record_news);
        $id_news = $this->db->insert_id();
        if(!empty($id_news)){
            if( !empty($data['language'])) unset($data['language']);
            $data['id_news'] = $id_news;
            $data['id_lang'] = $this->language;
            return $this->db->insert('news_lang',$data);
        }
        return false;
        
    }
    
    function update_news($data,$id){
        if(empty($data) || empty($id)) return false;
        $this->db->where('id_news',$id);
        //return $this->db->update('news_lang',$data);
        
        
        $get_one_lange = $this->get_onenews($id,true);
        if(!empty($get_one_lange)) return $this->db->update('news_lang',$data,array('id_news'=>$id,'id_lang'=>$this->language));
        else{
            $data['id_news'] = $id;
            $data['id_lang'] = $this->language;
            return $this->db->insert('news_lang',$data);
        }
    }
}

?>
